'use strict';

const { createBlogFailInfo } = require('../utils/ErrorModel');
const { ErrorModel, SuccessModel } = require(('../utils/resultModel'))
const PAGE_SIZE = 5;
// egg-tip
module.exports = app => {
  class BlogController extends app.Controller {

    async createBlog() {
      // 创建博客
      const { ctx } = this;
      // 3.获取提交的数据 内容和地址
      const { content, images } = ctx.request.body;
      // 还需要userId
      const { id: userId } = ctx.session.userInfo;
      try {

        // 4.将数据交给serveice处理 create
        const result = this.service.blog.create({ content, userId, images });
        ctx.body = new SuccessModel();
      } catch (err) {
        console.log(err);
        ctx.body = new ErrorModel(createBlogFailInfo);
      }
    }

    // // 加载我的空间更多
    async loadProfileMore() {
      const { ctx } = this
      ctx.body = { errno: 0, data: { u: 111 } }
      let { pageIndex, userName } = this.ctx.params;
      pageIndex = parseInt(pageIndex);
      // {userName: '小明',pageIndex:2}
      // 定义变量userId 值是用户的id
      // const { id: userId } = this.ctx.session.userInfo;
      const pageSize = PAGE_SIZE;
      // 获取博客信息 调用service中的方法
      const result = await this.service.blog.getProfileBlogList({ userName, pageIndex, pageSize });

      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }


    // 加载广场页更多
    async loadSquareMore() {
      let { pageIndex } = this.ctx.params;
      pageIndex = parseInt(pageIndex);
      // 定义每页显示数量
      const pageSize = 10;

      // 获取博客信息 调用service中的方法
      const result = await this.service.blog.getSquareBlogList({ pageIndex, pageSize });

      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }


    // 首页加载更多
    async loadHomeBlogMore() {
      // 在session中获取用户id
      const { id } = this.ctx.session.userInfo;
      // 获取pageIndex
      let { pageIndex } = this.ctx.params;
      pageIndex = parseInt(pageIndex);

      // 获取关注人博客                       getFollowerBlogList
      const result = await this.service.blog.getFollowerBlogList({
        userId: id,
        pageIndex,
      });
      console.log(result)
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize: 10,
      });


    };
  };

  return BlogController;
}